package zhoufu.sz.sanxiao_check.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import javax.inject.Inject;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.event.Event;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.StreetListModel;
import zhoufu.sz.sanxiao_check.presents.StreetPresenter;
import zhoufu.sz.sanxiao_check.ui.adapter.StreetAdapter;
import zhoufu.sz.sanxiao_check.ui.contract.StreetListContract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/21
 * 场所列表
 */
public class StreetListFragment extends BaseListFragment<StreetListModel>
        implements StreetListContract.View
{

    @Inject
    StreetPresenter _presenter;

    private String District;
    private String Grade;
    private String State;

    public static StreetListFragment newInstance(int statu, String Grade, String District)
    {
        StreetListFragment fragment = new StreetListFragment();
        fragment.District = District;
        fragment.Grade = Grade;
        fragment.State = statu+"";
        return fragment;
    }

    public void setArags(int statu, String District)
    {
        this.District = District;
        this.State = statu+"";
    }

    public static StreetListFragment newInstance(String Name, String Address, String Place_No,
            String Building_No)
    {
        Bundle args = new Bundle();
        args.putString("Name", Name);
        args.putString("Address", Address);
        args.putString("Place_No", Place_No);
        args.putString("Building_No", Building_No);
        StreetListFragment fragment = new StreetListFragment();
        fragment.setArguments(args);
        return fragment;
    }

  public static StreetListFragment newInstance(String Name, String Address, String Place_No,
      String Building_No,String type)
  {
    Bundle args = new Bundle();
    args.putString("Name", Name);
    args.putString("Address", Address);
    args.putString("Place_No", Place_No);
    args.putString("Building_No", Building_No);
    args.putString("Type", type);
    StreetListFragment fragment = new StreetListFragment();
    fragment.setArguments(args);
    return fragment;
  }

    @Override public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        LoginComponet componet = getComponent(LoginComponet.class);
        componet.inject(this);
        _presenter.bindView(this);
        _presenter.onStart();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout)
    {
        _presenter.setPageFirst();
        mAdapter.clear();
        _presenter.onStart();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout)
    {
        if ( mAdapter.getItemCount() < _presenter.getTotalSize() )
        {
            _presenter.next();
            return true;
        } else
        {
            return false;
        }
    }

    @Override protected void initAdapter()
    {
        mAdapter = new StreetAdapter(mAct, null, R.layout.item_street_list);
    }

    @Override public String Grade()
    {
        return TextUtils.isEmpty(Grade)?"0":Grade;
    }

    @Override public String District()
    {
        return TextUtils.isEmpty(District)?"0":District;
    }

    @Override public String Address()
    {
        return getArguments().getString("Address", "0");
    }

    @Override public String Place_No()
    {
        return getArguments().getString("Place_No", "0");
    }

    @Override public String Name()
    {
        return getArguments().getString("Name", "0");
    }

    @Override public String Building_No()
    {
        return getArguments().getString("Building_No", "0");
    }

    @Override public int Status()
    {
        return TextUtils.isEmpty(State)?0:Integer.parseInt(State);
    }

    @Override public void StreetStatuEvent(Event.RefreshStreetStatuEvent event)
    {
        mAdapter.clear();
        _presenter.setPageFirst();
        _presenter.setStatus(event.getStatus());
        _presenter.setDistrict(event.getDistrict());
        _presenter.onStart();
    }

    @Override public void PlaceChangeRefresh(Event.PlaceChangeRefresh event)
    {
        mAdapter.clear();
        _presenter.setPageFirst();
        _presenter.onStart();
    }

  @Override public String getType() {
    return getArguments().getString("Type", "0");
  }

  @Override public void onItemClick(View itemView, int viewType, int position)
    {
        mAct._navigator.startStreetDetailsAct(mAct, mAdapter.getList().get(position));
    }
}
